import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from "@angular/router";
import { NzMessageService } from "ng-zorro-antd";
import { NziModalService } from "ng-zorro-iop/modal/nzi-modal.service";
import { FcsService } from "../../service/fcs.service";
import { FcsTableComponent } from "../../shared-component/fcs-table/fcs-table.component";
import { Group, Region } from "../../shared-component/class/instance";
import { ApplicationCreateComponent } from "../create/application-create.component";

@Component({
  selector: 'app-application-detail',
  templateUrl: './application-detail.component.html',
  styleUrls: ['./application-detail.component.less']
})
export class ApplicationDetailComponent implements OnInit{
  @ViewChild('fcsTable', { static: false }) fcsTable: FcsTableComponent;
  groupId = '';
  studentValue;
  isLoading = true;
  data: Group;
  regionName: string;
  currentRegion: Region = {
    regionCode: '',
    regionName: ''
  };
  constructor(private fcsService: FcsService,
    private modalService: NziModalService,
    private msgService: NzMessageService,
    private activatedRoute: ActivatedRoute,
    private router: Router) { }

  ngOnInit() {
    this.groupId = this.activatedRoute.snapshot.params["id"];
    this.getApplicationDetail();
  }
  ngAfterViewInit() {
      this.fcsTable.getFunctionList(this.groupId);
  }
  public formChildValue(event: number) {
    this.studentValue = event;
    this.groupId = this.studentValue;

    this.router.navigate(['/application/detail/' + this.studentValue]);
    this.getApplicationDetail();
  }
  getApplicationDetail() {
    this.fcsService.getGroupDetail(this.groupId).subscribe(res => {
      this.data = res;
      this.regionName = this.data["regionName"];
      this.currentRegion.regionCode = this.data["regionCode"];
      this.currentRegion.regionName = this.data["regionName"];
      this.isLoading = false;
    }, error => {
      this.isLoading = false;
    })
  }

  createFunction(applicationId) {
    this.router.navigate(['/fcs-manage/function/create'], { queryParams: { applicationId: applicationId, regionCode: this.currentRegion.regionCode, regionName: this.currentRegion.regionName } });
  }

  editApplication(data) {
    this.modalService.create({
      nzTitle: '编辑应用',
      nzMaskClosable: false,
      nzWidth: '7rem',
      nzContent: ApplicationCreateComponent,
      nzComponentParams: { applicationInstance: data },
      nzOnOk: async createInstance => {
        let isSuccess = await createInstance.submitForm();
        isSuccess && this.getApplicationDetail();
        return isSuccess;
      },
    });
  }

  deleteApplication(data) {
    this.modalService.delete({
      nzTitle: '删除应用',
      nzContentTitle: `确定删除应用 <em>“${data.displayName}”</em> 吗？`,
      nzOnOk: () => {
        //返回一个promise对象才可以调用then等函数
        return new Promise((resolve, reject) => {
          this.fcsService.deleteGroup(data.id).subscribe(
            data => {
              if (data && data.code) {
                resolve(false);
                this.msgService.error(data.message || '删除失败！');
              } else {
                resolve(true);
                this.msgService.success('删除成功！');
                this.router.navigate(['/application']);
              }
            },
            err => {
              resolve(false);
            }
          );
        });
      },
      nzCancelText: '取消',
      nzOnCancel: () => { },
    });
  }
  // ngAfterViewChecked () {
  //   console.log('afterViewChecked');
  //   let targetE = document.getElementsByClassName("cdk-overlay-container")[0];
  //   if(targetE){
  //     targetE['style'].cssText += "width:50%!important;left:0;right:unset;";
  //   }
  // }
}
